// src/views/VehicleManagement.vue
<template>
  <div class="vehicle-management">
    <h1>车辆管理</h1>
    <el-button @click="handleOpenAddDialog">添加车辆</el-button>
    <el-table :data="vehicles" style="width: 100%">
      <el-table-column prop="licensePlate" label="车牌号码"></el-table-column>
      <el-table-column prop="vehicleType" label="车辆类型"></el-table-column>
      <el-table-column label="操作">
        <template v-slot:default="scope">
          <el-button @click="handleOpenEditDialog(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑车辆对话框 -->
    <el-dialog title="车辆信息" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="rules" ref="vehicleForm">
        <el-form-item label="车牌号码" prop="licensePlate">
          <el-input v-model="form.licensePlate"></el-input>
        </el-form-item>
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-input v-model="form.vehicleType"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveVehicle">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      vehicles: [],
      dialogVisible: false,
      form: {
        id: null,
        licensePlate: '',
        vehicleType: ''
      },
      rules: {
        licensePlate: [{ required: true, message: '请输入车牌号码', trigger: 'blur' }],
        vehicleType: [{ required: true, message: '请输入车辆类型', trigger: 'blur' }]
      }
    };
  },
  methods: {
    fetchVehicles() {
      axios.get('/api/vehicles/')
        .then(response => {
          this.vehicles = response.data;
        })
        .catch(error => {
          console.error('Error fetching vehicles:', error);
        });
    },
    handleOpenAddDialog() {
      this.form = { id: null, licensePlate: '', vehicleType: '' };
      this.dialogVisible = true;
    },
    handleOpenEditDialog(vehicle) {
      this.form = { ...vehicle };
      this.dialogVisible = true;
    },
    handleDelete(id) {
      axios.delete(`/api/vehicles/${id}`)
        .then(() => {
          this.fetchVehicles();  // 重新加载数据
          this.$message.success('车辆删除成功');
        })
        .catch(error => {
          console.error('Error deleting vehicle:', error);
          this.$message.error('车辆删除失败');
        });
    },
    saveVehicle() {
      const method = this.form.id ? 'put' : 'post';
      const url = this.form.id ? `/api/vehicles/${this.form.id}` : '/api/vehicles/';
      axios[method](url, this.form)
        .then(() => {
          this.dialogVisible = false;
          this.fetchVehicles();
          this.$message.success('车辆保存成功');
        })
        .catch(error => {
          console.error('Error saving vehicle:', error);
          this.$message.error('车辆保存失败');
        });
    }
  },
  created() {
    this.fetchVehicles();
  }
}
</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>
